What is the 32-bit representation of minus one?
The dump shows that the last integer, -1, is represented as FF FF FF FF
This is
32 one-bits, or 11111111111111111111111111111111
Here is the program again.
Now it writes out 512 int
s.
Also, a
BufferedOutputStream
is placed in the stream.
This buffers the bytes before they are written to disk.
In a program that writes only a few bytes, buffering adds little efficiency. But for a program that does massive I/O, buffering is essential. This program writes 512*4 == 2048 bytes, not really enough to worry about, but for practice, use a buffer.
import java.io.*; class WriteInts { public static void main ( String[] args ) { String fileName = "intData.dat" ; try { DataOutputStream out = new DataOutputStream( new BufferedOutputStream( new FileOutputStream( fileName ))); for ( int j=0; j<512; j++ ) out.writeInt( j ); out.close(); } catch ( IOException iox ) { System.out.println("Problem writing " + fileName ); } } }
DataOutputStream
has methods for
writing out short
, long
,
double
and other data.
What do you suppose the method that writes a double
is called?